<template>
  <div class="dashboard-container">
    <div class="search-container">
      <!-- 搜索列表 -->
      <el-form ref="formInline" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="检测点名称：" prop="monitorPoint">
          <el-input v-model="formInline.monitorPoint" placeholder="请输入检测点名称" />
        </el-form-item>
        <el-form-item label="检测点地址:" prop="monitorAddress">
          <el-input v-model="formInline.monitorAddress" placeholder="请输入检测点地址" />
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-input v-model="formInline.status" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="$refs['formInline'].resetFields()">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 添加按钮 -->
      <el-button type="primary" @click="dialogFormVisible = true">新建</el-button>
    </div>
    <!-- 数据列表 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        type="index"
        :index="handleI"
        label="编号"
        width="150"
      />
      <el-table-column
        prop="monitorPoint"
        label="检测点名称"
      />
      <el-table-column
        prop="monitorAddress"
        label="检测点地址"
      />
      <el-table-column
        prop="power"
        label="实时功率"
      />
      <el-table-column
        prop="voltage"
        label="实时电压"
      />
      <el-table-column
        prop="status"
        label="状态"
      />
      <el-table-column
        prop="reportTime"
        label="上报时间"
      />
      <el-table-column
        prop="notes"
        label="备注"
        show-overflow-tooltip
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="150"
      >
        <template #default="{row}">
          <el-button size="mini" @click="handleEdit(row.id)">修改</el-button>
          <el-button type="danger" size="mini" @click="handleDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 表单对话框 -->
    <el-dialog v-loading="dialogLoading" :title="title" :visible.sync="dialogFormVisible" width="30%" @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" :label-width="formLabelWidth">
        <el-form-item label="检测点名称：" prop="monitorPoint">
          <el-input v-model="form.monitorPoint" autocomplete="off" />
        </el-form-item>
        <el-form-item label="检测点地址：" prop="monitorAddress">
          <el-input v-model="form.monitorAddress" autocomplete="off" />
        </el-form-item>
        <el-form-item label="实时功率：" prop="power">
          <el-input v-model="form.power" autocomplete="off" />
        </el-form-item>
        <el-form-item label="实时电压：" prop="voltage">
          <el-input v-model="form.voltage" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-input v-model="form.status" autocomplete="off" />
        </el-form-item>
        <el-form-item label="备注：" prop="notes">
          <el-input v-model="form.notes" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDashs, delDash, addDash, getDash, updateDash } from '@/api/index'
import fetchData from '@/mixins/fetchData'
export default {
  name: 'Dashboard',
  mixins: [fetchData],
  data() {
    return {
      dialogFormVisible: false,
      dialogLoading: false,
      tableData: [],
      loading: false,
      form: {
        monitorPoint: '',
        monitorAddress: '',
        power: '',
        voltage: '',
        status: '',
        notes: ''
      },
      formLabelWidth: '120px',
      formInline: {
        monitorPoint: '',
        monitorAddress: '',
        status: ''
      },
      title: '',
      rules: {
        monitorPoint: [{ required: true, message: '请输入检测点名称', trigger: 'blur' }],
        monitorAddress: [{ required: true, message: '请输入检测点地址', trigger: 'blur' }],
        power: [{ required: true, message: '请输入实时功率：', trigger: 'blur' }],
        voltage: [{ required: true, message: '请输入实时电压', trigger: 'blur' }],
        status: [{ required: true, message: '请输入状态', trigger: 'blur' }]
      },
      currentPage: 1,
      total: 0,
      pageSize: 10
    }
  },
  created() {
    this.getData({
      pageSize: this.pageSize,
      pageNo: this.currentPage - 1
    })
  },
  methods: {
    async getData(params) {
      this.loading = true
      const res = await getDashs(params)
      this.tableData = res.content
      this.total = res.totalElements
      this.loading = false
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDash(id)
        if (this.tableData.length === 1) {
          this.currentPage--
        }
        await this.getData({
          pageSize: this.pageSize,
          pageNo: this.currentPage - 1
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    onSubmit() {
      this.getData(this.formInline)
    },
    handleAdd() {
      this.title = '添加能效检测点'
      this.$refs.form.validate(async(flag) => {
        if (flag) {
          if (this.form.id) {
            await updateDash(this.form)
            this.$message.success('修改成功')
          } else {
            await addDash(this.form)
            this.$message.success('添加成功')
          }
          this.dialogFormVisible = false
          this.getData({
            pageSize: this.pageSize,
            pageNo: this.currentPage - 1
          })
        }
      })
    },
    async handleEdit(id) {
      this.dialogFormVisible = true
      this.dialogLoading = true
      this.title = '修改能效检测点'
      this.form = await getDash(id)
      this.dialogLoading = false
    },
    handleClose() {
      this.$refs.form.resetFields()
      this.form = {
        monitorPoint: '',
        monitorAddress: '',
        power: '',
        voltage: '',
        status: '',
        notes: ''
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getData({
        pageSize: this.pageSize,
        pageNo: this.currentPage - 1
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData({
        pageSize: this.pageSize,
        pageNo: this.currentPage - 1
      })
    },
    handleI(i) {
      return this.pageSize * (this.currentPage - 1) + i + 1
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
  .search-container {
    box-sizing: border-box;
    padding:20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    ::v-deep .el-form-item {
      margin-bottom: 0;
    }
  }
  .notes {
    overflow: hidden;
  }
}
</style>
